package com.apps.ftapp.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.SubjectMoreAdapter;
import com.apps.ftapp.adapter.SubjectMoreGridAdapter;
import com.apps.ftapp.model.SubjectMoreModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.Singleton;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class SubjectMoreActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.gridView)
    GridView gridView;
    @BindView(R.id.goal_title)
    TextView goalTitle;
    @BindView(R.id.goal_reg_title)
    TextView goalRegTitle;
    @BindView(R.id.goal_reg_title_mubiao)
    TextView goalRegTitleMubiao;
    @BindView(R.id.activity_subject_more)
    RelativeLayout activitySubjectMore;


    private Gson gson = new Gson();

    private List<SubjectMoreModel.InfoBean.CategoryBean> dataList = new ArrayList();
    private SubjectMoreAdapter subjectMoreAdapter;


    private SubjectMoreGridAdapter subjectMoreGridAdapter;
    private List<String> gridList = new ArrayList();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_more);
        ButterKnife.bind(this);

        setTitle("考试目标");

        initView();


        Typeface typeFace = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Hiragino Sans GB W6.otf");
        goalTitle.setTypeface(typeFace);
        goalRegTitle.setTypeface(typeFace);


        loadData();


    }

    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("page", 1);
        Async.post("course/category", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //SubjectModel
                    SubjectMoreModel result = gson.fromJson(new String(responseBody), SubjectMoreModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList.addAll(result.getInfo().getCategory());
                        subjectMoreAdapter.addRes(result.getInfo().getCategory());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }


    private void initView() {

        subjectMoreAdapter = new SubjectMoreAdapter(getApplicationContext(), dataList);
        listView.setAdapter(subjectMoreAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> maps = new HashMap();
                maps.put("name", dataList.get(i).getName());

                Singleton.getInstance().clearMap();
                Singleton.getInstance().clearTit();
                Map m = new HashMap();
                m.put("id", dataList.get(i).getId());
                m.put("name", dataList.get(i).getName());
                Singleton.getInstance().addMap(m);
                Singleton.getInstance().setCategory(dataList.get(i).getId());
                Singleton.getInstance().setTit(dataList.get(i).getName());

                Singleton.getInstance().setYiji(m);
                L.i("main", "........" + dataList.get(i).getName());
//                SPUtils.put(getApplicationContext(),"yiji_id",dataList.get(i).getId());
                SharedPreferencesUtil.saveData(getApplicationContext(), "yijiId", dataList.get(i).getId());
                SharedPreferencesUtil.saveData(getApplicationContext(), "yijiName", dataList.get(i).getName());
//                SPUtils.put(getApplicationContext(),"yiji_name",dataList.get(i).getName());

                ActivitySkipUtil.skipAnotherActivity(SubjectMoreActivity.this, SubjectMore1Activity.class, true);
            }
        });

        subjectMoreGridAdapter = new SubjectMoreGridAdapter(getApplicationContext(), gridList);
        gridView.setAdapter(subjectMoreGridAdapter);
        gridView.setVisibility(View.GONE);
    }
}
